package vn.co.thn.android.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListViewCustom extends ListView implements OnScrollListener {
	LayoutInflater mLayoutInflater;
	//
	private int mListType = 0;
	// PullToRefresh
	private static final int TAP_TO_REFRESH = 1;
	private static final int PULL_TO_REFRESH = 2;
	private static final int RELEASE_TO_REFRESH = 3;
	protected static final int REFRESHING = 4;

	protected static final String TAG = "PullToRefreshListView";

	private OnRefreshListener mOnRefreshListener;

	/**
	 * Listener that will receive notifications every time the list scrolls.
	 */
	protected LayoutInflater mInflater;

	// header
	private View mRefreshView;
	private TextView mRefreshViewText;
	private ImageView mRefreshViewImage;
	private ProgressBar mRefreshViewProgress;
	private TextView mRefreshViewLastUpdated;

	protected int mCurrentScrollState;
	protected int mRefreshState;

	private RotateAnimation mFlipAnimation;
	private RotateAnimation mReverseFlipAnimation;

	private int mRefreshViewHeight;
	private int mRefreshOriginalTopPadding;
	private int mLastMotionY;

	private boolean mBounceHack;
	// LoadMore
	private View mFooterView;
	private boolean isLoadMore = false;
	private boolean isLoading = false;
	private OnLoadMoreListener mOnLoadMoreListener;
	private int mCountRowFirst = 0;
	private int mMaxItem = 0;
	private int mRow_Limit = 0;

	public ListViewCustom(Context context) {
		super(context);
	}

	public ListViewCustom(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public ListViewCustom(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	protected void init(Context context, AttributeSet attrs) {
		//
		TypedArray a = null;
		try {
			a = context.obtainStyledAttributes(attrs,
					R.styleable.ListViewCustom);
			mListType = a.getInteger(R.styleable.ListViewCustom_ListType, 0);
		} finally {
			if (a != null)
				a.recycle();
		}
		mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		switch (mListType) {
		case 1:
			setUpFooter();
			hideFooter();
			setOnScrollListener(this);
			break;
		case 2:
			setupHeader(context);
			setOnScrollListener(this);
			break;
		case 3:
			setupHeader(context);
			setUpFooter();
			hideFooter();
			setOnScrollListener(this);
			break;
		}
	}

	private void setupHeader(Context context) {
		// Load all of the animations we need in code rather than through XML
		mFlipAnimation = new RotateAnimation(0, -180,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		mFlipAnimation.setInterpolator(new LinearInterpolator());
		mFlipAnimation.setDuration(250);
		mFlipAnimation.setFillAfter(true);
		mReverseFlipAnimation = new RotateAnimation(-180, 0,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
		mReverseFlipAnimation.setDuration(250);
		mReverseFlipAnimation.setFillAfter(true);

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// header
		mRefreshView = (RelativeLayout) mInflater.inflate(
				R.layout.header_listview, this, false);
		mRefreshViewText = (TextView) mRefreshView
				.findViewById(R.id.pull_to_refresh_text);
		mRefreshViewImage = (ImageView) mRefreshView
				.findViewById(R.id.pull_to_refresh_image);
		mRefreshViewProgress = (ProgressBar) mRefreshView
				.findViewById(R.id.pull_to_refresh_progress);
		mRefreshViewLastUpdated = (TextView) mRefreshView
				.findViewById(R.id.pull_to_refresh_updated_at);

		mRefreshViewImage.setMinimumHeight(50);
		mRefreshView.setOnClickListener(new OnClickRefreshListener());
		mRefreshOriginalTopPadding = mRefreshView.getPaddingTop();

		mRefreshState = TAP_TO_REFRESH;

		addHeaderView(mRefreshView);

		measureView(mRefreshView);
		mRefreshViewHeight = mRefreshView.getMeasuredHeight();
	}

	private void setUpFooter() {
		mFooterView = mLayoutInflater.inflate(R.layout.footer_listview, this,
				false);
		addFooterView(mFooterView);
	}

	private void showFooter() {
		if (mListType == 1 | mListType == 3) {
			this.mFooterView.findViewById(R.id.footer).setVisibility(
					View.VISIBLE);

		}
	}

	private void hideFooter() {
		if (mListType == 1 | mListType == 3) {
			this.mFooterView.findViewById(R.id.footer).setVisibility(View.GONE);
			isLoadMore = false;
		}
	}

	@Override
	protected void onAttachedToWindow() {
		// have to ask super to attach to window, otherwise it won't scroll in
		// jelly bean.
		super.onAttachedToWindow();
		if (mListType == 1|mListType == 0) {
			setSelection(0);
		} else {
			setSelection(1);
		}
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
		if (mListType == 1|mListType == 0) {
			setSelection(0);
		} else {
			setSelection(1);
		}
	}

	/**
	 * Set the listener that will receive notifications every time the list
	 * scrolls.
	 * 
	 * @param l
	 *            The scroll listener.
	 */

	/**
	 * Register a callback to be invoked when this list should be refreshed.
	 * 
	 * @param onRefreshListener
	 *            The callback to run.
	 */
	public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
		mOnRefreshListener = onRefreshListener;
	}

	/**
	 * Set a text to represent when the list was last updated.
	 * 
	 * @param lastUpdated
	 *            Last updated at.
	 */
	public void setLastUpdated(CharSequence lastUpdated) {
		if (lastUpdated != null) {
			mRefreshViewLastUpdated.setVisibility(View.VISIBLE);
			mRefreshViewLastUpdated.setText(lastUpdated);
		} else {
			mRefreshViewLastUpdated.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mListType == 2 | mListType == 3) {
			final int y = (int) event.getY();
			mBounceHack = false;

			switch (event.getAction()) {
			case MotionEvent.ACTION_UP:
				if (!isVerticalScrollBarEnabled()) {
					setVerticalScrollBarEnabled(true);
				}
				if (getFirstVisiblePosition() == 0
						&& mRefreshState != REFRESHING) {
					if ((mRefreshView.getBottom() >= mRefreshViewHeight || mRefreshView
							.getTop() >= 0)
							&& mRefreshState == RELEASE_TO_REFRESH) {
						// Initiate the refresh
						mRefreshState = REFRESHING;
						prepareForRefresh();
						onRefresh();
					} else if (mRefreshView.getBottom() < mRefreshViewHeight
							|| mRefreshView.getTop() <= 0) {
						// Abort refresh and scroll down below the refresh view
						resetHeader();
						setSelection(1);
					}
				}
				break;
			case MotionEvent.ACTION_DOWN:
				mLastMotionY = y;
				break;
			case MotionEvent.ACTION_MOVE:
				applyHeaderPadding(event);
				break;
			}

		}
		return super.onTouchEvent(event);
	}

	private void applyHeaderPadding(MotionEvent ev) {
		// getHistorySize has been available since API 1
		int pointerCount = ev.getHistorySize();

		for (int p = 0; p < pointerCount; p++) {
			if (mRefreshState == RELEASE_TO_REFRESH) {
				if (isVerticalFadingEdgeEnabled()) {
					setVerticalScrollBarEnabled(false);
				}

				int historicalY = (int) ev.getHistoricalY(p);

				// Calculate the padding to apply, we divide by 1.7 to
				// simulate a more resistant effect during pull.
				int topPadding = (int) (((historicalY - mLastMotionY) - mRefreshViewHeight) / 1.7);

				mRefreshView.setPadding(mRefreshView.getPaddingLeft(),
						topPadding, mRefreshView.getPaddingRight(),
						mRefreshView.getPaddingBottom());
			}
		}
	}

	/**
	 * Sets the header padding back to original size.
	 */
	private void resetHeaderPadding() {
		mRefreshView.setPadding(mRefreshView.getPaddingLeft(),
				mRefreshOriginalTopPadding, mRefreshView.getPaddingRight(),
				mRefreshView.getPaddingBottom());
	}

	/**
	 * Resets the header to the original state.
	 */
	private void resetHeader() {
		if (mRefreshState != TAP_TO_REFRESH) {
			mRefreshState = TAP_TO_REFRESH;

			resetHeaderPadding();

			// Set refresh view text to the pull label
			mRefreshViewText.setText(R.string.pull_to_refresh_tap_label);
			// Replace refresh drawable with arrow drawable
			mRefreshViewImage
					.setImageResource(R.drawable.ic_pulltorefresh_arrow);
			// Clear the full rotation animation
			mRefreshViewImage.clearAnimation();
			// Hide progress bar and arrow.
			mRefreshViewImage.setVisibility(View.GONE);
			mRefreshViewProgress.setVisibility(View.GONE);
		}
	}

	private void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// When the refresh view is completely visible, change the text to say
		// "Release to refresh..." and flip the arrow drawable.
		if (mListType == 2 | mListType == 3) {
			if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
					&& mRefreshState != REFRESHING) {
				if (firstVisibleItem == 0) {
					mRefreshViewImage.setVisibility(View.VISIBLE);
					if ((mRefreshView.getBottom() >= mRefreshViewHeight + 20 || mRefreshView
							.getTop() >= 0)
							&& mRefreshState != RELEASE_TO_REFRESH) {
						mRefreshViewText
								.setText(R.string.pull_to_refresh_release_label);
						mRefreshViewImage.clearAnimation();
						mRefreshViewImage.startAnimation(mFlipAnimation);
						mRefreshState = RELEASE_TO_REFRESH;
					} else if (mRefreshView.getBottom() < mRefreshViewHeight + 20
							&& mRefreshState != PULL_TO_REFRESH) {
						mRefreshViewText
								.setText(R.string.pull_to_refresh_pull_label);
						if (mRefreshState != TAP_TO_REFRESH) {
							mRefreshViewImage.clearAnimation();
							mRefreshViewImage
									.startAnimation(mReverseFlipAnimation);
						}
						mRefreshState = PULL_TO_REFRESH;
					}
				} else {
					mRefreshViewImage.setVisibility(View.GONE);
					resetHeader();
				}
			} else if (mCurrentScrollState == SCROLL_STATE_FLING
					&& firstVisibleItem == 0 && mRefreshState != REFRESHING) {
				setSelection(1);
				mBounceHack = true;
			} else if (mBounceHack && mCurrentScrollState == SCROLL_STATE_FLING) {
				setSelection(1);
			}
		}
		// LoadMore
		if (mListType == 1 | mListType == 3) {
			if (mOnLoadMoreListener == null) {
				return;
			}
			mCountRowFirst = visibleItemCount + firstVisibleItem;
			mMaxItem = totalItemCount;
			if (mListType == 1 | mListType == 3) {
				if (mCountRowFirst >= totalItemCount && mCountRowFirst > 0
						&& totalItemCount >= mRow_Limit) {
					isLoadMore = true;
					if (visibleItemCount == 2) {
						hideFooter();
						return;

					}
					if (!isLoading) {
						showFooter();
					}
				}
			}
		}
	}

	public void setRowLimit(int rowlimit) {
		this.mRow_Limit = rowlimit;
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		mCurrentScrollState = scrollState;
		if (mListType == 2 | mListType == 3) {
			if (mCurrentScrollState == SCROLL_STATE_IDLE) {
				mBounceHack = false;
			}
		}
		// loadmore
		if (mListType == 1 | mListType == 3) {
			if (mOnLoadMoreListener == null) {
				return;
			}
			if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
				if (mMaxItem == mCountRowFirst && mMaxItem > 0
						&& mMaxItem > mRow_Limit) {
					isLoadMore = true;
					if (!isLoading) {
						showFooter();

					}
					view.invalidate();
					setSelectionFromTop(mMaxItem, 0);
				}
			}
			if (scrollState == SCROLL_STATE_IDLE
					| SCROLL_STATE_FLING == scrollState) {
				if (isLoadMore && mMaxItem == mCountRowFirst && mMaxItem > 0) {
					view.invalidate();
					if (!isLoading) {
						isLoading = true;
						mOnLoadMoreListener.onLoadMore();
					}
				}
			}
		}
	}

	public void prepareForRefresh() {
		resetHeaderPadding();

		mRefreshViewImage.setVisibility(View.GONE);
		// We need this hack, otherwise it will keep the previous drawable.
		mRefreshViewImage.setImageDrawable(null);
		mRefreshViewProgress.setVisibility(View.VISIBLE);

		// Set refresh view text to the refreshing label
		mRefreshViewText.setText(R.string.pull_to_refresh_refreshing_label);

		mRefreshState = REFRESHING;
	}

	public void onRefresh() {
		Log.d(TAG, "onRefresh");

		if (mOnRefreshListener != null) {
			mOnRefreshListener.onRefresh();
		}
	}

	/**
	 * Resets the list to a normal state after a refresh.
	 * 
	 * @param lastUpdated
	 *            Last updated at.
	 */
	public void onRefreshComplete(CharSequence lastUpdated) {
		setLastUpdated(lastUpdated);
		onRefreshComplete();
	}

	/**
	 * Resets the list to a normal state after a refresh.
	 */
	public void onRefreshComplete() {
		Log.d(TAG, "onRefreshComplete");

		resetHeader();

		// If refresh view is visible when loading completes, scroll down to
		// the next item.
		if (mRefreshView.getBottom() > 0) {
			invalidateViews();
			setSelection(1);
		}
	}

	/**
	 * Invoked when the refresh view is clicked on. This is mainly used when
	 * there's only a few items in the list and it's not possible to drag the
	 * list.
	 */
	private class OnClickRefreshListener implements OnClickListener {

		public void onClick(View v) {
			if (mRefreshState != REFRESHING) {
				prepareForRefresh();
				onRefresh();
			}
		}

	}

	// Loadmore
	public void onLoadMoreComplete() {
		hideFooter();
		isLoadMore = false;
		isLoading = false;
//		setSelection(mMaxItem);
	}

	// handler loadMore
	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	//
	/**
	 * Interface definition for a callback to be invoked when list should be
	 * refreshed.
	 */
	// interface
	public interface OnLoadMoreListener {
		/**
		 * Called when the list reaches the last item (the last item is visible
		 * to the user)
		 */
		public void onLoadMore();

	}

	public interface OnRefreshListener {
		/**
		 * Called when the list should be refreshed.
		 * <p>
		 * A call to {@link PullToRefreshListView #onRefreshComplete()} is
		 * expected to indicate that the refresh has completed.
		 */
		public void onRefresh();
	}

}
